<template>
	<div class="container">
		<el-row>
			<el-col class="userTitle">
				<p>用户列表</p>
			</el-col>
		</el-row>
		<section class="userContent">
			<div class="addUser">
				<el-row>
					<el-col :span="1">
						<el-button type="primary" size="small" @click="goAddUser">
							<i class="fa fa-plus" aria-hidden="true"></i>
							<span>新增用户</span>
						</el-button>
					</el-col>
				</el-row>
			</div>

			<div class="searchForm">
				<el-form :model="queryForm" label-width="80px">
					<el-row>
						<el-col :span="5">
							<el-form-item label="账号名称">
								<el-input size="small" v-model="queryForm.accountName"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="5">
							<el-form-item label="机构名称">
								<el-input size="small" v-model="queryForm.insitutionName"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="5">
							<el-form-item label="联系方式">
								<el-input size="small" v-model="queryForm.telephone"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="5">
							<el-form-item label="职位信息">
								<el-select size="small" v-model="queryForm.position">
									<el-option label="查询条件一" value="0"></el-option>
									<el-option label="查询条件二" value="1"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="4">
							<el-form-item>
								<el-button size="small" type="primary" @click="searchUser">查询</el-button>
							</el-form-item>
						</el-col>
					</el-row>

				</el-form>
			</div>

			<div class="userInfo">
				<el-table :data="tableData" border style="width: 100%">
					<el-table-column align="left" label="账号名称" width="130">
						<template scope="scope">
							<span>{{ scope.row.accountName }}</span>
						</template>
					</el-table-column>
					<el-table-column align="left" label="姓名" width="130">
						<template scope="scope">
							<span>{{scope.row.userName}}</span>
						</template>
					</el-table-column>
					<el-table-column align="left" label="所属机构" :show-overflow-tooltip="true" width="230">
						<template scope="scope">
							<span>{{scope.row.institutionName}}</span>
						</template>
					</el-table-column>
					<el-table-column align="left" label="职位信息" width="170">
						<template scope="scope">
							<span>{{scope.row.position}}</span>
						</template>
					</el-table-column>
					<el-table-column align="left" label="联系方式" width="150">
						<template scope="scope">
							<span>{{scope.row.telephone}}</span>
						</template>
					</el-table-column>
					<el-table-column label="操作" align="left">
						<template scope="scope">
							<el-button class="edit" size="small" type="text" @click.native.prevent="handleEdit(scope.$index, scope.row)"><i class="el-icon-edit"></i>编辑</el-button>
							<el-button class="delete" size="small" type="text" @click.native.prevent="deleteUser(scope.$index, scope.row)"><i class="el-icon-delete"></i>删除</el-button>
						</template>
					</el-table-column>
				</el-table>
			</div>

			<div class="block">
		    <el-pagination
		      @size-change="handleSizeChange"
		      @current-change="handleCurrentChange"
		      :current-page.sync="currentPage"
		      :page-size="10"
		      layout="total, prev, pager, next"
		      :total= "total">
		    </el-pagination>
			</div>
		</section>
	</div>
</template>

<style lang="scss" scoped>
.container {
  .userTitle {
    width: 100%;
    text-align: left;
    font-size: 22px;
    color: #444;
    height: 60px;
    border-bottom: 4px solid #cdd1da;
    line-height: 60px;
    padding-left: 20px;
  }
  .userContent {
    padding: 10px;
    background: #fff;
    .addUser {
      margin: 3px 10px 10px;
    }
    .searchForm {
      .searchForm .el-col {
        height: 45px;
      }
    }
  }
  .userInfo {
    .el-table {
      font-size: 13px;
    }
    .edit {
      color: #3c8dbc;
    }
    .delete {
      color: #ff4949;
    }
  }
  .block {
    text-align: right;
    padding-top: 13px;
  }
}
</style>

<script>
import { getUserListInfo, searchUser } from "../../api/api";
export default {
  data() {
    return {
      queryForm: {
        insitutionName: "",
        userName: "",
        telephone: "",
        position: ""
      },
      tableData: [{}],
      total: 1,
      currentPage: 1
    };
  },
  methods: {
    goAddUser() {
      //前往新增用户页
      this.$router.push({
        path: "/userAdd"
      });
    },
    getUserList() {
      //查询用户列表
      var _this = this;
      let paramData = {
        pageIndex: this.currentPage,
        pageSize: 10
      };
      getUserListInfo(paramData).then(function(res) {
        _this.tableData = res.data.data;
        _this.total = res.data.totalNum;
      });
    },
    handleSizeChange(val) {
      //每页数量改变时会触发
    },
    handleCurrentChange(val) {
      //翻页改变时会触发
      this.getUserList();
    },

    searchUser() {
      //查询操作
      var _this = this;
      let paramData = {
        pageIndex: 0,
        pageSize: 10,
        insitutionName: this.queryForm.institutionName,
        accountName: this.queryForm.accountName,
        telephone: this.queryForm.telephone,
        position: this.queryForm.position
      };
      searchUser(paramData).then(function(res) {
        if (res.data.status === "200") {
          _this.tableData = res.data.data;
          _this.total = res.data.totalNum;
        }
      });
    },
    handleEdit(index, row) {
      //编辑操作
      this.$router.push({
        path: "/userEdit/" + row.id
      });
    },
    deleteUser(index, row) {
      //删除操作
      var _this = this;
      this.$confirm("此操作将永久删除该条信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          _this.axios
            .delete("/account/delete/?ids=" + row.id)
            .then(function(res) {
              if (res.data.status === "200") {
                _this.$message({
                  type: "success",
                  message: "删除成功!"
                });
                _this.getUserList();
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  },
  mounted() {
    this.getUserList();
  }
};
</script>